Skip to content

Add pytest test suite with mocked GitHub API responses - #3

Merged
Vedant-code merged 1 commit into
Vedant-code:mainfrom
AshayK003:pr/test-harness
Jul 7, 2026
Merged

Add pytest test suite with mocked GitHub API responses#3
Vedant-code merged 1 commit into
Vedant-code:mainfrom
AshayK003:pr/test-harness

Conversation

@AshayK003

Copy link
Copy Markdown
Contributor

Summary

Sets up a proper test harness for the GitHub search crawler. Zero tests was the biggest quality gap — this adds 9 focused tests covering the core wait_for_rate_limit, fetch_page, and crawl_github_algorithm_issues functions.

What was added

tests/conftest.py — shared fixtures

  • MockResponse — builds a mock requests.Response with the shape the code expects (status_code, headers, json(), text)
  • make_search_result() — builds GitHub Search API result bodies
  • mock_requests fixture — patches requests.get with configurable return value
  • mock_sleep fixture — patches time.sleep to a no-op so tests finish in ~0.1s instead of minutes
  • empty_env / success_response fixtures for common setups

tests/test_main.py — 9 tests covering:

Test What it verifies
test_uses_reset_ts wait_for_rate_limit sleeps until X-RateLimit-Reset
test_fallback_60s Falls back to 60s when no headers present
test_success_returns_response 200 returns the response object
test_403_calls_wait_and_retries 403 → rate-limit wait → retry → success
test_422_returns_none Rejected query returns None without retry
test_unexpected_status_returns_none 500 returns None
test_timeout_retries requests.exceptions.Timeout caught and retried
test_max_retries_exceeded Returns None after exhausting retries
test_produces_output_file Full crawl writes correct output to results.md

pyproject.toml — added [dev] optional dependency group with pytest>=8 and [tool.pytest.ini_options].

Running

pip install -e ".[dev]"
pytest tests/ -v

Design choices

  • Zero external test dependencies — only pytest itself. Uses unittest.mock (stdlib), no pytest-mock or responses needed.
  • Mock-based, not integration — no real GitHub API calls, no network. Tests run in ~0.1s.
  • time.sleep is patched — rate-limit tests would take minutes of wall time otherwise. With mock_sleep they finish instantly.
  • 9/9 pass — verified on Python 3.11.

Sets up a tests/ package with conftest.py fixtures (MockResponse helper,
mock_requests patch, mock_sleep patch) and 9 focused tests covering:
- wait_for_rate_limit with X-RateLimit-Reset header and no-header fallback
- fetch_page success, 403 retry, 422 rejection, 500 rejection
- requests.exceptions.Timeout and ConnectionError retry logic
- Full crawl_github_algorithm_issues output-file production

Dev dependency group [dev] with pytest>=8 added to pyproject.toml.
Install with: pip install -e ".[dev]"

@Vedant-code Vedant-code left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a solid foundation that addresses the "zero tests" problem. Great work. However, it leaves some important paths untested (global state management, _check_rate_limit_before_request, retry-after header).

I am opening a follow-up issue to add the missing tests (especially the global state reset and _check_rate_limit_before_request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants